import RankModel, { RankItemVo } from "../RankModel";
import { Util } from "../../../utils/Util";
import { GameVoManager } from "../../../manager/GameVoManager";
import { AlertManager } from "../../../alert/AlertManager";
import { Notifier } from "../../../framework/Notifier";
import { ListenID } from "../../../ListenID";
import FightModel from "../../fight/FightModel";
import GlobalData from "../../../GlobalData";
import { UIManager } from "../../../framework/UIManager";
import { Common_UIPath } from "../../../common/Common_Define";


const { ccclass, property } = cc._decorator;

@ccclass
export default class RankItem extends cc.Component {

    @property(cc.Sprite)
    bgSp: cc.Sprite = null;

    @property(cc.Sprite)
    rankIcon: cc.Sprite = null;

    @property(cc.Label)
    nick_name: cc.Label = null;

    @property(cc.Label)
    scoreText: cc.Label = null;

    @property(cc.Label)
    rankText: cc.Label = null;

    @property(cc.Sprite)
    avatar: cc.Sprite = null;

    @property(cc.Node)
    tiaozhan: cc.Node = null;

    private sf: cc.SpriteFrame;
    protected start(): void {
        this.sf = this.avatar.spriteFrame;
    }

    private _data: RankItemVo;
    protected changeListener(enable: boolean): void {
        // Notifier.changeListener(enable, NotifyID.SDK_Banner_Resize, this.resizeBtnPos, this);
    }

    private _itemId: number = 0;

    updateItem(index: number, realindex: number, stageMap: cc.SpriteAtlas) {
        this._itemId = realindex;
        this.updateData(realindex, stageMap);
    }

    public get itemID(): number {
        return this._itemId;
    }


    rankdata: any;
    updateData(rank: number, stageMap: cc.SpriteAtlas, type: number = 2) {
        if (rank > 2) {
            this.rankText.string = `${rank + 1}`;
            this.rankText.node.active = true;
            this.rankIcon.node.active = false;
            this.bgSp.spriteFrame = stageMap.getSpriteFrame("img_board4");
        } else {
            this.rankIcon.node.active = true;
            this.rankText.node.active = false;
            this.rankIcon.spriteFrame = stageMap.getSpriteFrame('img_medal' + (rank + 1));
            this.bgSp.spriteFrame = stageMap.getSpriteFrame("img_board" + (rank + 1));
        }
        let rankdata = RankModel.getInstance.rankBattleList[rank] as any;
        if (type == 2) {
            rankdata = RankModel.getInstance.rankStageList[rank];
        } else if (type == 3) {
            rankdata = RankModel.getInstance.rankCoinList[rank];
        } else if (type == 4) {
            rankdata = RankModel.getInstance.rankFriendList[rank];
        }
        this.rankdata = rankdata;
        // this.tiaozhan.active = type !== 3;
        this.tiaozhan.active = true;
        this._data = rankdata;
        this.avatar.spriteFrame = this.sf;
        // console.log("排行榜更新数据", rankdata)
        if (rankdata) {
            if (rankdata.user_nickname) {
                if (String(rankdata.user_nickname).length > 10) {
                    this.nick_name.string = String(rankdata.user_nickname).substring(0, 10) + "...";
                } else {
                    this.nick_name.string = rankdata.user_nickname;
                }
            } else {
                this.nick_name.string = '匿名' + rank;
            }


            Util.loadRemoteSf(rankdata.avatar, this.avatar);
            this.setStageInfo(rankdata, stageMap, type);
        } else {
            this.nick_name.string = "虚位以待";
            this.scoreText.string = `--`;
            // if (page == 2) {
            //     this.scoreText.string = `--`;
            // } else if (page == 3) {
            //     this.scoreText.string = `--`;
            // }
        }
    }

    updateMyData(rankdata: any, stageMap: cc.SpriteAtlas, curRankType: number = 2): void {

        // this.nick_name.string = GameVoManager.getInstance.myUserVo.nick_name;
        this.nick_name.string = String(GameVoManager.getInstance.myUserVo.nick_name).substring(0, 10) + "...";

        this.scoreText.string = `${rankdata.sumdiffbattle || GameVoManager.getInstance.myUserVo.battle}`;
        this.bgSp.spriteFrame = stageMap.getSpriteFrame("img_board4");

        this.avatar.spriteFrame = this.sf;
        Util.loadRemoteSf(rankdata.avatar, this.avatar);

        let rank = rankdata["myrank"];
        // if (curRankType == 2) {
        //     // let chaptes = Util.levelToChapterId(RankModel.getInstance.myStageScore);
        //     // this.scoreText.string = `第${chaptes[0]}章${chaptes[1]}波`;
        //     // this.setStageInfo(rankdata, stageMap, page);
        //     let score = rankdata["topLevel"];
        //     score = Number(score);
        //     let chaptes = Util.levelToChapterId(score);
        //     let stepNum = FightModel.getInstance.curStep;
        //     this.scoreText.string = `第${chaptes[0]}章${chaptes[1]}波`;
        // } else if (curRankType == 3) {
        //     this.scoreText.string = `${RankModel.getInstance.myBossScore}层`;
        // }
        if (curRankType == 3) {
            // 修复 NaN 问题
            const battleScore = Number(rankdata["sumdiffcoin"]) || 0;
            let goldFormat = Util.goldFormat(battleScore);
            // this.scoreText.string = rankdata["sumdiffcoin"];
            this.scoreText.string = goldFormat + "";
        }
        if (rankdata) {
            if (rank > 2) {
                this.rankText.node.active = true;
                this.rankIcon.node.active = false;
                if (rank > 10000) {
                    this.rankText.string = '10000+';
                } else {
                    this.rankText.string = `${rank}`;
                }
            } else {
                this.rankIcon.node.active = true;
                this.rankText.node.active = false;
                this.rankIcon.spriteFrame = stageMap.getSpriteFrame('img_medal' + (rank));
                if (rank < 5) {
                    this.bgSp.spriteFrame = stageMap.getSpriteFrame("img_board" + (rank));
                } else {
                    this.bgSp.spriteFrame = stageMap.getSpriteFrame("img_board4");
                }
            }
        } else {
            if (rank > 3) {
                this.rankText.node.active = true;
                this.rankIcon.node.active = false;
                if (rank > 42) {
                    // this.rankText.string = '100+';
                    this.rankText.string = "--";
                } else {
                    this.rankText.string = `${rank}`;
                }
            } else {
                this.rankText.node.active = false;
                this.rankIcon.node.active = true;
            }
            // this.bgSp.spriteFrame = stageMap.getSpriteFrame("img_board4");
        }
    }


    setStageInfo(rankdata: RankItemVo, stageMap: cc.SpriteAtlas, type: number = 2) {
        let score = rankdata["sumdiffbattle"];
        this.scoreText.string = `${score}`;
        // if (type == 2) {
        //     let score = rankdata["topLevel"];
        //     score = Number(score);
        //     let chaptes = Util.levelToChapterId(score);
        //     let stepNum = FightModel.getInstance.curStep;
        //     // this.scoreText.string = `第${chaptes[0]}章${chaptes[1]}关`;
        //     this.scoreText.string = `第${chaptes[0]}章${chaptes[1]}波`;
        // } else if (type == 3) {
        //     this.scoreText.string = `${score}层`;
        // }
        if (type == 3) {
            let goldFormat = Util.goldFormat(Number(rankdata["sumdiffcoin"]));
            // this.scoreText.string = rankdata["sumdiffcoin"];
            this.scoreText.string = goldFormat + "";
        }
    }

    registerTouch() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchstart, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchend, this);
    }

    public touchstart() {

    }

    public touchend() {
        if (this._data && this._data.data) {
            try {
                let userinfo = JSON.parse(this._data.data);
                if (userinfo && userinfo.equip_part) {
                    Notifier.send(ListenID.Log_Event, { event_name: "rankPage_details" });
                    Notifier.send(ListenID.Person_OpenOtherInfoView, userinfo);
                } else {
                    AlertManager.showNormalTips("数据暂未更新，无法查看详情");
                }
            } catch (error) {

            }
        }
    }

    public onClose(): void {
        UIManager.Close(Common_UIPath.RankUI);
        UIManager.Close(Common_UIPath.MenuUI);
        UIManager.Close(Common_UIPath.CurrencyUI);
        this.unscheduleAllCallbacks();
        Notifier.send(ListenID.Menu_Open, false);
        Notifier.send(ListenID.On_Close_Menu);
    }

    onClickTiaozhan() {
        if (GlobalData.inst.uid == this.rankdata.id) {
            AlertManager.showNormalTips("不能挑战自己");
            return;
        }
        if (GameVoManager.getInstance.myUserVo.gold < 5000) {
            AlertManager.showNormalTips("金币不足5k");
            return;
        }
        this.onClose();
        GlobalData.inst.tiaozhandata.default_gunId = this.rankdata.default_gunId || 105;
        GlobalData.inst.tiaozhandata.battle = Number(this.rankdata.battle) || 100;
        GlobalData.inst.tiaozhandata.uid = this.rankdata.id || "";
        GlobalData.inst.tiaozhandata.baseAllHp = Number(this.rankdata.baseAllHp) || 300;
        GlobalData.inst.tiaozhandata.type = "rank"
        cc.director.loadScene("GameTiaozhan");
    }
}
